package com.freesun.shop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freesun.shop.anno.Log;
import com.freesun.shop.entity.SysUser;
import com.freesun.shop.service.SysRoleService;
import com.freesun.shop.entity.SysRole;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @ApiOperation("查询角色的列表")
    @GetMapping("/list")
    @Log(name = "查询角色的列表")
    public ResponseEntity<List<SysRole>> findRoleList(){

        List<SysRole> list = roleService.list();

        return ResponseEntity.ok(list);
    }
    @ApiOperation("分页条件查询角色")
    @GetMapping("/page")
    @Log(name = "分页条件查询角色")
    public ResponseEntity<IPage<SysRole>> page(SysRole sysRole, Page<SysRole> page){

        IPage<SysRole> sysRoleIPage = roleService.page(sysRole,page);

        return ResponseEntity.ok(sysRoleIPage);
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping
    @Log(name = "删除了角色")
    public ResponseEntity<Void> batchDeleteRole(@RequestBody List<Long> ids){

        //删除角色不仅删除角色，还有用户角色关系表
        roleService.removeByIds(ids);

        return ResponseEntity.ok().build();
    }
    @ApiOperation("新增角色")
    @PostMapping
    @Log(name = "新增角色")
    public ResponseEntity<Void> addRole(@RequestBody SysRole sysRole){
        System.out.println("sysRole = " + sysRole);
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysRole.setCreateUserId(user.getUserId());
        roleService.save(sysRole);
        return ResponseEntity.ok().build();
    }
    @ApiOperation("查询单个角色")
    @GetMapping("/info/{id}")
    @Log(name = "查询单个角色")
    public ResponseEntity<SysRole> findById(@PathVariable("id") Long id){
        SysRole sysRole = roleService.getById(id);
        return ResponseEntity.ok(sysRole);
    }
    @ApiOperation("修改角色")
    @PutMapping
    @Log(name = "修改角色")
    public ResponseEntity<Void> updateRole(@RequestBody SysRole sysRole){
        roleService.updateById(sysRole);
        return ResponseEntity.ok().build();
    }
}
